//
//  CertificateCenterDetailsController.m
//  VideoExam
//
//  Created by apple on 2020/5/13.
//

#import "CertificateCenterDetailsController.h"

@interface CertificateCenterDetailsController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView * scrollView;
@property(nonatomic,strong)UIImageView * imageView;
@end

@implementation CertificateCenterDetailsController
- (void)setupNavigationItems {
    [super setupNavigationItems];
    [self isShowNavBack_pop];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    UIImageView * imageView =[UIFactory create_ImageView_ImageName:@""];
//    [imageView sd_setImageWithURL:[NSURL URLWithString:self.certificateUrl]];
//    [self.view addSubview:imageView];
//    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.left.mas_equalTo(0);
//        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH*0.58));
//        make.centerY.mas_equalTo(self.view);
//    }];
    
    
    
   
   
    
    
    /*首先，本文要实现的效果类似于微信个人中心中查看自己的头像，具体功能点如下：
     1.进入界面原始状态为image居中显示
     2.当scale==1时，双击放大图片到：高度等于屏幕高度，同时等比缩放宽度，放大后，竖直方向不可滚动，水平可以
     3.当scale>1时，双击复原
     4.捏合手势缩放后，zoomScale在min与max之间，这时保持imageView竖直居中
     */
    self.scrollView = [[UIScrollView alloc] init];
    self.imageView.frame = self.view.bounds;
    self.scrollView.backgroundColor = UIColor.blackColor;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                             action:@selector(doubleTapScrollV:)];
    tapGes.numberOfTapsRequired = 2;
    [self.scrollView addGestureRecognizer:tapGes];
    
    //这里有个小tips，[[UIImageView alloc] initWithImage:]这个方法，
    //初始化出来的imageView.frame是等于(0, 0, image.size.width, image.size.height)的
    //之前一直以为是CGRectZero...
    self.imageView = [[UIImageView alloc] init];
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.certificateUrl]];
    [self.scrollView addSubview:self.imageView];
    
    //想要做到放大后刚好竖直方向撑满屏幕，那么maxScale就等于SCREEN_HEIGHT/SCREEN_WIDTH
    CGFloat maxScale = SCREEN_HEIGHT/SCREEN_WIDTH;
    self.scrollView.maximumZoomScale = maxScale;
    self.scrollView.minimumZoomScale = 1;
    //scrollView是全屏大小
    self.scrollView.frame = UIScreen.mainScreen.bounds;
    
    //imageView居中,与scrollView等宽
    self.imageView.frame = CGRectMake(0, 0, self.scrollView.width, self.scrollView.width *0.58);
    self.imageView.center = CGPointMake(self.scrollView.center.x, self.scrollView.center.y-NavigationBarHeight);
    
    UIButton * btn = [UIFactory create_btnWithTitle:@"" imageName:@"24_nor" backgroundColor:nil TypefaceColor:theme_color fontSize:14 target:self action:@selector(backClicked)];
        [self.view addSubview:btn];
       [btn mas_makeConstraints:^(MASConstraintMaker *make) {
           make.top.mas_equalTo(15+StatusBarHeight);
           make.left.mas_equalTo(15);
           make.width.mas_equalTo(30);
           make.height.mas_equalTo(30);
       }];
    
}
//返回需要缩放的子视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return self.imageView;
}
//scrollView进行了缩放
//通过方法zoomToRect来缩放时，这个方法只会调用一次，调用时间点在scrollViewWillBeginZooming之后，scrollViewDidEndZooming之前
//使用双指捏合手势缩放中，这个方法会一直调用,缩放中,scrollView的contentSize会变化,即放大缩小
//如果内容宽(高)小于scrollView宽(高),内容以scrollView宽(高)为准居中
//如果内容宽(高)大于scrollView宽(高),内容从x=0(y=0)开始
//如此便达到了,内容任一方向小于父容器时,居中该方向,
//大于父容器时,内容占满该方向
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    UIView *targetView = [self viewForZoomingInScrollView:scrollView];
    BOOL widthIsSmall = targetView.width < scrollView.width;
    BOOL heightIsSmall = targetView.height < scrollView.height;
    
    if (widthIsSmall) {
        targetView.centerX = scrollView.centerX;
    } else {
        targetView.left = 0;
    }
    
    if (heightIsSmall) {
        targetView.centerY = scrollView.centerY-NavigationBarHeight;
    } else {
        targetView.top = 0;
    }
}

- (void)doubleTapScrollV:(UITapGestureRecognizer*)tapGes{
    CGFloat currScale = self.scrollView.zoomScale;
    CGFloat minScale = self.scrollView.minimumZoomScale;
    CGFloat maxScale = self.scrollView.maximumZoomScale;
    CGFloat goalScale;
    
    if (currScale == minScale) {
        goalScale = maxScale;
    } else {
        goalScale = minScale;
    }
    
    if (currScale == goalScale) {
        return;
    }
    
    CGPoint touchPoint = [tapGes locationInView:[self viewForZoomingInScrollView:self.scrollView]];
    //缩放后的目标frame大小,当然是通过容器(scrollView)的size/scale来计算的,如下:
    CGFloat xsize = self.scrollView.frame.size.width/goalScale;
    CGFloat ysize = self.scrollView.frame.size.height/goalScale;
    //origin当然是点击点减去一半的宽高,
    //x,y可能小于0,但是没问题,因为内容不能从负值开始,如下
    CGFloat x = touchPoint.x-xsize/2;
    CGFloat y = touchPoint.y-ysize/2;
    
    [self.scrollView zoomToRect:CGRectMake(x, y, xsize, ysize) animated:YES];
}

/// 设置导航栏底部的分隔线图片，默认为 NavBarShadowImage，必须在 navigationBar 设置了背景图后才有效（系统限制如此）
- (nullable UIImage *)navigationBarShadowImage {
    return  [UIImage qmui_imageWithColor:[UIColor whiteColor] size:CGSizeMake(SCREEN_WIDTH, 1) cornerRadius:0];
}

- (UIColor *)navigationBarTintColor {
    return [UIColor whiteColor];
}
- (void)backClicked {
    [self dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
